package com.changhong.framework.sdfs.namenode.server.datanode;

/**
 * 用于数据节点的对象
 * @author summer
 */
public class DataNodeLease<T> {
    private static final long duration=30 * 1000;
    private T holder;
    private long registrationTimestamp;
    private volatile long lastUpdateTimestamp;

    public DataNodeLease(T r) {
        holder = r;
        registrationTimestamp = System.currentTimeMillis();
        lastUpdateTimestamp = registrationTimestamp;
    }
    public void renew() {
        lastUpdateTimestamp = System.currentTimeMillis();
    }


    public boolean isExpired() {
        return isExpired(0L);
    }

    public boolean isExpired(long additionalLeaseMs) {
        return (System.currentTimeMillis() > (lastUpdateTimestamp + additionalLeaseMs + duration));
    }

    public long getRegistrationTimestamp() {
        return registrationTimestamp;
    }

    public long getLastRenewalTimestamp() {
        return lastUpdateTimestamp;
    }

    public T getHolder() {
        return holder;
    }

}
